package com.zmn.plat.services.impl.category;

import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.manager.interfaces.cache.CategoryCache;
import com.zmn.plat.model.entity.category.BaseCategory;
import com.zmn.plat.model.entity.category.BaseCategoryQuery;
import com.zmn.plat.model.entity.category.CategoryInfoDTO;
import com.zmn.plat.persistence.interfaces.category.CategoryDao;
import com.zmn.plat.services.interfaces.category.BaseCategoryService;

/**
 * @author duanzuocai
 * @version v1.0
 * @since 2018/10/19 9:25
 */
@Service
public class BaseCategoryServiceImpl implements BaseCategoryService {
    @Resource
    CategoryCache categoryCache;
    @Resource
    CategoryDao categoryDao;

    @Override
    public BaseCategory findByKey(Integer categoryId) {
        if (NumberUtil.isPositiveInteger(categoryId)) {
            return categoryDao.findByKey(categoryId);
        }
        return null;
    }

    @Override
    public BaseCategory findByKeyOfCache(Integer categoryId) {
        if (NumberUtil.isPositiveInteger(categoryId)) {
            return categoryCache.getCategory(categoryId);
        }
        return null;
    }

    @Override
    public BaseCategory findByKeyNoCache(Integer categoryId) {
        return findByKey(categoryId);
    }

    @Override
    public List<BaseCategory> findByKeys(List<Integer> categoryId) {
        return categoryDao.findByKeys(categoryId);
    }

    @Override
    public BaseCategory findByName(String name) {
        return categoryDao.findByName(name);
    }

    @Override
    public BaseCategory findShowCategoryByCategId(Integer categId) {
        return categoryDao.findShowCategoryByCategId(categId);
    }

    @Override
    public List<BaseCategory> listPageByQuery(BaseCategoryQuery query) {
        Integer count = categoryDao.countByQuery(query);
        query.setTotalCount(count);
        return categoryDao.listPageByQuery(query);
    }

    @Override
    public List<BaseCategory> listPageByFaultQuery(BaseCategoryQuery query) {
        Integer count = categoryDao.countByFaultQuery(query);
        query.setTotalCount(count);
        return categoryDao.listPageByFaultQuery(query);
    }

    @Override
    public List<VtDTO> listByParentId(Integer parentId) {
        return categoryCache.getCategoryList(parentId);
    }

    @Override
    public List<BaseCategory> listByParentId(int parentId, int statusEnable) {
        BaseCategoryQuery query = new BaseCategoryQuery();
        query.setStatus(statusEnable);
        query.setParentId(parentId);
        query.setShowType(ProductConsts.ERP_PRODUCT_TYPE);

        return categoryDao.listByQuery(query);
    }

    @Override
    public List<BaseCategory> listByParentIdForShow(int parentId, int statusEnable) {
        BaseCategoryQuery query = new BaseCategoryQuery();
        query.setStatus(statusEnable);
        query.setParentId(parentId);
        query.setShowType(ProductConsts.EC_PRODUCT_TYPE);

        return categoryDao.listByQuery(query);
    }

    @Override
    public List<BaseCategory> listByQuery(BaseCategoryQuery query) {
        return categoryDao.listByQuery(query);
    }


    @Cacheable(key = "'baseCategory:all'", cacheNames = "redis5m")
    @Override
    public List<BaseCategory> listAll() {

        return categoryDao.listAll();
    }

    // @Override
    // public List<ProductTariffGroupItemVo> listCategoryByShowProductId(ProductTariffGroupItemQuery productFaultItemQuery) {
    // return categoryDao.listCategoryByShowProductId(productFaultItemQuery);
    // }

    @Override
    public Integer findMaxSort(Integer showType, Integer level) {
        return categoryDao.findMaxSort(showType, level);
    }

    @Override
    public Integer countByQuery(BaseCategoryQuery query) {
        return categoryDao.countByQuery(query);
    }

    @Override
    public Integer countByFaultQuery(BaseCategoryQuery query) {
        return categoryDao.countByFaultQuery(query);
    }

    @Override
    public List<BaseCategory> listOneCategoryByChannelIdAndServCategId(Integer channelId, Integer showType, Integer servCategId) {
        if (Objects.equals(showType, ProductConsts.ERP_PRODUCT_TYPE)) {
            return categoryDao.listOneCategoryByChannelIdAndServCategId(channelId, servCategId);
        } else {
            return categoryDao.listOneShowCategoryByChannelIdAndServCategId(channelId, servCategId);
        }
    }

    @Override
    public List<CategoryInfoDTO> listCategoryByChannelIdAndCategOneId(Integer channelId, Integer showType, Integer categOneId) {
        if (Objects.equals(showType, ProductConsts.ERP_PRODUCT_TYPE)) {
            return categoryDao.listCategoryByChannelIdAndCategOneId(channelId, categOneId);
        } else {
            return categoryDao.listShowCategoryByChannelIdAndCategOneId(channelId, categOneId);
        }
    }

    @Override
    public List<CategoryInfoDTO> listCategoryByChannelIdAndServCategId(Integer channelId, Integer showType, Integer servCategId) {
        if (Objects.equals(showType, ProductConsts.ERP_PRODUCT_TYPE)) {
            return categoryDao.listShowCategoryByChannelIdAndServCategId(channelId, servCategId);
        } else {
            return categoryDao.listCategoryByChannelIdAndServCategId(channelId, servCategId);
        }
    }

    @Override
    public List<CategoryInfoDTO> listShowCategByChannelId(Integer channelId) {
        return categoryDao.listShowCategByChannelId(channelId);
    }

    @Override
    public List<CategoryInfoDTO> listCategoryByChannelIdAndCityIdAndServCategId(Integer channelId, Integer cityId, Integer servCategId) {
        return categoryDao.listCategoryByChannelIdAndCityIdAndServCategId(channelId, cityId, servCategId);
    }

    @Override
    public List<CategoryInfoDTO> listCategoryByShowCategoryUnionIds(String[] unionIds) {
        return categoryDao.listCategoryByShowCategoryUnionIds(unionIds);
    }

    @Override
    public List<CategoryInfoDTO> listCategoryByChannelIdAndServCategIds(Integer channelId, Integer showType, List<Integer> servCategIds) {
        return categoryDao.listCategoryByChannelIdAndServCategIds(channelId, showType, servCategIds);
    }

    @Override
    public List<CategoryInfoDTO> listCategoryByChannelIdAndCityIdAndServCategIds(Integer channelId, Integer cityId, List<Integer> servCategIds) {
        return categoryDao.listCategoryByChannelIdAndCityIdAndServCategIds(channelId, cityId, servCategIds);
    }

    @Override
    @CacheEvict(cacheNames = "redis1d", key = "'category:id:'+#category.categId", condition = "#category.categId > 0")
    public Integer updateByKey(BaseCategory category) {
        return categoryDao.updateByKey(category);
    }

    @Override
    public Integer updateSortKey(int categId, int sort, String updater) {
        BaseCategory category = new BaseCategory();
        category.setCategId(categId);
        category.setSort(sort);
        category.setUpdater(updater);
        category.setUpdateTime(DateUtil.getNow());
        return categoryDao.updateSortKey(category);
    }

    @Override
    public Integer updateFirstLetterByKey(int categId, String firstLetter, String updater) {
        BaseCategory category = new BaseCategory();
        category.setCategId(categId);
        category.setFirstLetter(firstLetter);
        category.setUpdater(updater);
        category.setUpdateTime(DateUtil.getNow());
        return categoryDao.updateFirstLetterByKey(category);
    }

    @Override
    public Integer updateSimpleSpellKey(Integer categId, String firstLetter, String updater) {
        BaseCategory category = new BaseCategory();
        category.setCategId(categId);
        category.setSimpleSpell(firstLetter);
        category.setUpdater(updater);
        category.setUpdateTime(DateUtil.getNow());
        return categoryDao.updateSimpleSpellKey(category);
    }

    @SuppressWarnings("Duplicates")
    @Override
    public Integer updateStatusBatch(List<Integer> arrayIds, int status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(arrayIds);
        dto.setStatus(status);
        dto.setUpdater(updater);
        dto.setUpdateTime(DateUtil.getNow());

        int relust = categoryDao.updateStatusBatch(dto);

        return relust;
    }

    @Override
    public Integer updateTariffStatusBatch(List<Integer> categoryIds, int status, String mcStaffName, Integer productType) {
        return categoryDao.updateTariffStatusBatch(categoryIds, status, mcStaffName, productType);
    }

    @Override
    @CacheEvict(cacheNames = "redis1d", key = "'category:pid:'+#category.parentId", condition = "#category.parentId != null")
    public Integer insert(BaseCategory category) {
        if (category.getParentId() == null) {
            category.setParentId(GlobalConsts.TOP_ID);
        }
        return categoryDao.insert(category);
    }
}
